package com.rabbitmq.rabbit;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * @Author: TongRui乀
 * @Date: 2019/8/2 11:20
 * @description： 订单消息监听
 */
@Component()
@Slf4j
public class Order2MessageListener  {

    @RabbitListener(queues = "${order2.queue.name}", concurrency = "6", containerFactory = "multiListenerContainer")
    public void onMessage(Message message, Channel channel) throws Exception {

        long tag = message.getMessageProperties().getDeliveryTag();

        try {
            String msg = new String(message.getBody());

            log.info("order2 listener receive message :[{}] channel number is [{}]", msg, channel.getChannelNumber());

            // 消息确认 成功消费
            channel.basicAck(tag, true);
        }catch (Exception e){
            // 消息确认 消费失败
            channel.basicAck(tag, false);
            e.printStackTrace();
        }

    }
}
